using System;
using System.Collections.Generic;
using System.Text;
using T.FAS.Runtime.Metadata.Entity.DataModel;
using T.FAS.Runtime.Base.Entity;
using T.FAS.Runtime.DataAccess.Common;
using T.FAS.Runtime.DataAccess.Engine.ValidateChecker.Entity;

namespace T.FAS.Runtime.DataAccess.Engine
{
    /// <summary>
    /// DQ运行时校验类
    /// </summary>
    public class DQValidateChecker
    {
        public static ItemCheckResult Check(DataQueryParam param, object inputValue)
        {
            ValidateCheckItem checkitem = new ValidateCheckItem()
            {
                InputValue = Convert.ToString(inputValue),
            };
            checkitem.CheckElement = ConvertTo(param);
            return checkitem.CheckNull().CheckDataType().Result;
        }

        private static IValidateCheckElement ConvertTo(DataQueryParam param)
        {
            if(!Enum.TryParse(param.ParamType, true, out DataType dataType))
            {
                throw new FasException(DataAccessErrorCode.DataQuery.UnvalidParam, string.Format(CommonStringClassEngine.DQValidateChecker_ConvertTo2,param.DataQueryID,(param.IsInputParam ? CommonStringClassEngine.DataAccessEngine_Argument : CommonStringClassEngine.DataAccessEngine_Variable),param.Code,param.ParamType));
            }
            return new ValidateCheckElement
            {
                Name = param.Name,
                DataType = dataType,
                DefaultValue = param.IsInputParam ? null : param.DefaultValue,
                IsAllowNull = param.IsAllowNull,
                Length = 0,
                Precision = 0,
            };
        }
    }
}
